<?php
/**
 * Created by PhpStorm.
 * User: Kevin
 * Date: 2018/12/18
 * Time: 11:51
 */
namespace app\admin\controller;
use think\Db;
class Banner extends Base
{
    /*
     * 轮播图数据
     */
    public function index(){
        if(request()->isAjax ()){
            extract(input());
            $map = [];
            if(isset($type)&&$type != ""){
                $map['lunbo_type'] = $type;
            }
            if(isset($key)&&$key!="")
            {
                $map['lunbo_content'] = ['like',"%" . $key . "%"];
            }
            $Nowpage = input('page') ? input('page'):1;
            $limits = input("limit")?input("limit"):10;// 获取总条数;
            $count = Db::name('lunbo')->where($map)->count();
            $lists = Db::name('lunbo')
                ->where($map)
                ->field(['lunbo_id','lunbo_type','lunbo_content','lunbo_image','lunbo_update_time','lunbo_create_time'])
                ->page($Nowpage,$limits)
                ->order('lunbo_create_time desc')
                ->select ();
            return json(['code'=>220,'msg'=>'','count'=>$count,'data'=>$lists]);
        }
        return $this->fetch('banner/index');
    }

    /*
     * 添加轮播图
     */
    public function bannerAdd(){
        if(request()->isPost ()){
            extract(input());
            $time = date('Y-m-d H:i:s');
            $param = [
                'lunbo_type'=>$type,
                'lunbo_content'=>$content,
                'lunbo_image'=>$img,
                'lunbo_create_time'=>$time,
                'lunbo_update_time'=>$time
            ];
            $res = Db::name('lunbo')->insert ($param);
            if($res){
                return json(['code'=>200,'msg'=>'添加成功']);
            }else{
                return json(['code'=>100,'msg'=>'添加失败']);
            }
        }
        return $this->fetch('banner/banner_add');
    }

    /*
     * 编辑轮播图
     */
    public function bannerEdit(){
        if(request()->isPost ()){
            extract(input());
            $param = [
                'lunbo_type'=>$type,
                'lunbo_content'=>$content,
                'lunbo_image'=>$img,
                'lunbo_update_time'=>date('Y-m-d H:i:s')
            ];
            $res = Db::name('lunbo')->where('lunbo_id',$id)->update ($param);
            if($res){
                return json(['code'=>200,'msg'=>'修改成功']);
            }else{
                return json(['code'=>100,'msg'=>'修改失败']);
            }
        }
        $info = Db::name('lunbo')
            ->where(['lunbo_id'=>input('id')])
            ->field(['lunbo_id','lunbo_type','lunbo_content','lunbo_image'])
            ->find();
        return $this->fetch('banner/banner_edit',['info'=>$info]);
    }

    /*
     * 删除轮播图
     */
    public function bannerDel(){
        $res = Db::name('lunbo')->where('lunbo_id',input('id'))->delete ();
        if($res){
            return json(['code'=>200,'msg'=>'删除成功']);
        }else{
            return json(['code'=>100,'msg'=>'删除失败']);
        }
    }
}